package org.adw.launcher2.ld.secret.ui.adapter;

import java.util.LinkedList;
import java.util.List;

import org.adw.launcher2.R;
import org.adw.launcher2.ld.secret.dom.MySMS;
import org.adw.launcher2.ld.secret.ui.ViewConversationSMSActivity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MySMSAdapter extends ArrayAdapter<MySMS> {

	private Context context;
	private LayoutInflater inflater;
	public static List<MySMS> listToHide = null;
	public MySMSAdapter(Context context, int textViewResourceId,
			List<MySMS> list) {
		super(context, textViewResourceId, list);
		this.context = context;
		inflater = LayoutInflater.from(context);
		listToHide = new LinkedList<MySMS>();

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		// if(convertView==null){
		convertView = inflater.inflate(R.layout.contact_info_item, null);
		holder = new ViewHolder();
		holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
		holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);

		convertView.setTag(holder);
		// }else{
		// holder = (ViewHolder) convertView.getTag();
		// }
		final MySMS userSMS = getItem(position);
		holder.txtName.setText(userSMS.userNickname);
		holder.checkBox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							if (!listToHide.contains(userSMS)) {
								listToHide.add(getItem(position));
							}
						} else {
							listToHide.remove(getItem(position));
						}
					}
				});
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (context.toString().contains("MyHiddenSMSActivity")) {
					Intent intent = new Intent(context,
							ViewConversationSMSActivity.class);
					intent.putExtra("ID", userSMS.userId);
					context.startActivity(intent);
				}
			}
		});
		return convertView;
	}

	static class ViewHolder {
		TextView txtName;
		CheckBox checkBox;
	}
}
